#include "StdAfx.h"
#include "RangeContainer.h"
#include "Puppet.h"


CRangeContainer::RangeID CRangeContainer::AddRange(const SRange& range)
{
	assert(m_ranges.size() <= std::numeric_limits<RangeID>::max());
	RangeID id = m_ranges.size();
	m_ranges.push_back(range);
	return id;
}

void CRangeContainer::Update(CPuppet* pPuppet)
{
	if (IAIObject* pAttTarget = pPuppet->GetAttentionTarget())
	{
		float distToTargetSq = pPuppet->GetPos().GetSquaredDistance(pAttTarget->GetPos());

		Ranges::iterator it = m_ranges.begin();
		Ranges::iterator end = m_ranges.end();

		for ( ; it != end; ++it)
		{
			SRange& range = *it;

			SRange::State newState = (distToTargetSq < range.rangeSq) ? SRange::Inside : SRange::Outside;

			if (newState != range.state)
			{
				range.state = newState;

				if (newState == SRange::Inside)
				{
					if (!range.enterSignal.empty())
					{
						pPuppet->SetSignal(1, range.enterSignal.c_str(), NULL, NULL, 0);
					}
				}
				else
				{
					if (newState == SRange::Outside)
					{
						if (!range.leaveSignal.empty())
						{
							pPuppet->SetSignal(1, range.leaveSignal.c_str(), NULL, NULL, 0);
						}
					}
				}
			}
		}
	}
}
